<?php
/**
 * Created by PhpStorm.
 * User: yiyu
 * Email: smalleasyu@gmail.com
 * Date: 2020-01-28 12:00:27
 * Desc:
 */

namespace app\admin\controller\goods;

use app\admin\controller\adminAuthController;
use app\base\model\goods\GoodsBaseInfo;
use app\admin\service\GoodsService;
use app\base\model\goods\GoodsPlatformInfo;
use app\core\exception\{BusinessException, ErrorCode};
use app\core\log\Log4dd;
use think\Request;

class GoodsController extends adminAuthController
{

    public function getGoodsList()
    {
        $size = input('page_size/d', 20);
        $where = [];
        $order = [];
        $goods_name = input('goods_name/s', '');
        if ($goods_name) {
            $where['gbi.goods_name'] = ['like', "%$goods_name%"];
        }

        $platform_name = input('platform_name/s', '');
        if ($platform_name) {
            $where['gpi.platform_name'] = ['like', "%$platform_name%"];
        }

        $min_platform_price = input('min_platform_price/s', '');
        $max_platform_price = input('max_platform_price/s', '');
        if ($max_platform_price && $min_platform_price) {
            $where['gpi.platform_price'] = ['BETWEEN', [(float)$min_platform_price, (float)$max_platform_price]];
        } elseif ($min_platform_price) {
            $where['gpi.platform_price'] = ['>=', $min_platform_price];
        } elseif ($max_platform_price) {
            $where['gpi.platform_price'] = ['<=', $max_platform_price];
        }

        $goods_status = input('goods_status/d', 0);
        if ($goods_status) {
            $where['gbi.goods_status'] = $goods_status;
        }

        $sort = input('sort/d', 0);

        if ($sort) {
            switch ($sort) {
                case 1:
                    $order = ['gbi.create_time' => 'desc'];
                    break;
                case 2:
                    $order = ['gbi.create_time' => 'asc'];
                    break;
                case 3:
                    $order = ['gbi.goods_stock' => 'desc'];
                    break;
                case 4:
                    $order = ['gbi.goods_stock' => 'asc'];
                    break;
                case 5:
                    $order = ['gsi.initiate_num' => 'desc'];
                    break;
                case 6:
                    $order = ['gsi.initiate_num' => 'asc'];
                    break;
                case 7:
                    $order = ['gsi.success_num' => 'asc'];
                    break;
                case 8:
                    $order = ['gsi.success_num' => 'desc'];
                    break;
                case 9:
                    $order = ['gsi.visit_num' => 'asc'];
                    break;
                case 10:
                    $order = ['gsi.visit_num' => 'desc'];
                    break;
                case 11:
                    $order = ['gbi.need_wish_coin' => 'asc'];
                    break;
                case 12:
                    $order = ['gbi.need_wish_coin' => 'desc'];
                    break;
            }
        } else {
            $order = ['gbi.sort_num' => 'asc'];
        }

        $data = GoodsBaseInfo::adminGetGoodsList($where, $order, $size);
        return $this->successReturn($data);

    }


    public function addGoods()
    {
        Log4dd::info('新增商品', Request::instance()->param(false));
        $goods_name = input('goods_name/s', '');

        if (empty($goods_name)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品名称不能为空');
        }

        $goods_title = input('goods_title/s', '');

        /*if (empty($goods_title)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品标题不能为空');
        }*/

        $goods_sub_title = input('goods_sub_title/s', '');

        if (empty($goods_sub_title)) {
            //throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品副标题不能为空');
        }

        $goods_img_url = input('goods_img_url/s', '');

        if (empty($goods_img_url)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品图片不能为空');
        }

        $platform_price = input('platform_price/f', 0.00);
        if (empty($platform_price)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '第三方平台的价格不能为0');
        }

        $platform_name = input('platform_name/s', '');
        if (empty($platform_name)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '第三方平台名称不能为为空');
        }

        $platform_url = input('platform_url/s', '');
        if (empty($platform_url)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '第三方平台地址不能为为空');
        }



        $need_people = input('need_people/d', 0);
        $need_wish_coin = input('need_wish_coin/f', 0);
        $goods_category_id = input('goods_category_id/d', 0);
        $people_get_wish_coin = input('people_get_wish_coin/f', 0);
        $goods_label = input('goods_label/s', '');
        $goods_specification = input('goods_specification/s', '');
        $goods_stock = input('goods_stock/d', 0);
        $goods_status = input('goods_status/d', 0);
        $open_start_time = input('open_start_time/s', '1970-01-01 00:00:00');
        $show_start_time = input('show_start_time/s', '1970-01-01 00:00:00');
        $show_end_time = input('show_end_time/s', '1970-01-01 00:00:00');
        $sort_num = input('sort_num/d', 0);

        //介绍图编排信息
        $introduce_content_info = input('introduce_content_info/a', []);
        $introduce_carousel_info = input('introduce_carousel_info/a', []);

        //搜索关键字，多个以,隔开
        $keywords = input('keywords/s', '');

        $show_platform_price = input('show_platform_price/d', 0);
        $can_free = input('can_free/d', 0);

        $data = [
            'base_info' => compact('goods_category_id', 'goods_name', 'goods_title', 'goods_sub_title', 'goods_img_url', 'goods_specification',
                'need_wish_coin', 'need_people', 'people_get_wish_coin', 'goods_stock', 'goods_label',
                'goods_status', 'open_start_time', 'show_start_time', 'show_end_time', 'sort_num'),
            'platform_info' => compact('platform_name', 'platform_price', 'platform_url'),
            'introduce_info' => compact('introduce_content_info', 'introduce_carousel_info'),
            'keyword_info' => compact('keywords'),
            'config_info' => compact('show_platform_price', 'can_free')
        ];

        try {
            GoodsService::addNewGoods($data);
            return $this->successReturn();
        } catch (BusinessException $e) {
            Log4dd::error("新增新商品失败:{$e->getMessage()}", $data, $e);
            return $this->errorReturn($e->getCode(), $e->getMessage());
        }
    }

    public function updateGoods()
    {
        Log4dd::info('修改商品', Request::instance()->param(false));
        $goods_id = input('goods_id/d', 0);

        if (empty($goods_id)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品id不能为空');
        }

        $goods_info = GoodsBaseInfo::find($goods_id);

        if (empty($goods_info)) {
            throw new BusinessException(ErrorCode::DATA_NOT_FOUND, '商品不存在');
        }

        $goods_name = input('goods_name/s', $goods_info['goods_name']);

        if (empty($goods_name)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品名称不能为空');
        }

        $goods_title = input('goods_title/s', $goods_info['goods_title']);

        /*if (empty($goods_title)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品标题不能为空');
        }*/

        $goods_sub_title = input('goods_sub_title/s', $goods_info['goods_sub_title']);

        if (empty($goods_sub_title)) {
            //throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品副标题不能为空');
        }

        $goods_img_url = input('goods_img_url/s', $goods_info['goods_img_url']);

        if (empty($goods_img_url)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '商品图片不能为空');
        }

        $platform_info = GoodsPlatformInfo::find($goods_id);

        if (empty($platform_info)) {
            throw new BusinessException(ErrorCode::DATA_NOT_FOUND, '原第三方信息不存在');
        }

        $platform_price = input('platform_price/f', $platform_info['platform_price']);
        if (empty($platform_price)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '第三方平台的价格不能为0');
        }

        $platform_name = input('platform_name/s', $platform_info['platform_name']);
        if (empty($platform_name)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '第三方平台名称不能为为空');
        }

        $platform_url = input('platform_url/s', $platform_info['platform_url']);
        if (empty($platform_url)) {
            throw new BusinessException(ErrorCode::COMMON_LACK_PARAMS, '第三方平台地址不能为为空');
        }

        $need_people = input('need_people/d', $goods_info['need_people']);

        $need_wish_coin = input('need_wish_coin/f', $goods_info['need_wish_coin']);
        $goods_category_id = input('goods_category_id/d', $goods_info['goods_category_id']);
        $people_get_wish_coin = input('people_get_wish_coin/f', $goods_info['people_get_wish_coin']);
        $goods_label = input('goods_label/s', $goods_info['goods_label']);
        $goods_specification = input('goods_specification/s', $goods_info['goods_specification']);
        $goods_stock = input('goods_stock/d', $goods_info['goods_stock']);
        $goods_status = input('goods_status/d', $goods_info['goods_status']);
        $open_start_time = input('open_start_time/s', $goods_info['open_start_time']);
        $show_start_time = input('show_start_time/s', $goods_info['show_start_time']);
        $show_end_time = input('show_end_time/s', $goods_info['show_end_time']);
        $sort_num = input('sort_num/d', $goods_info['sort_num']);

        //介绍图编排信息
        $introduce_content_info = input('introduce_content_info/a', []);
        $introduce_carousel_info = input('introduce_carousel_info/a', []);

        //搜索关键字，多个以,隔开
        $keywords = input('keywords/s', '');

        $show_platform_price = input('show_platform_price/d', 0);
        $can_free = input('can_free/d', 0);

        $data = [
            'base_info' => compact('goods_category_id', 'goods_name', 'goods_title', 'goods_sub_title', 'goods_img_url', 'goods_specification',
                'need_wish_coin', 'need_people', 'people_get_wish_coin', 'goods_stock', 'goods_label',
                'goods_status', 'open_start_time', 'show_start_time', 'show_end_time', 'sort_num'),
            'platform_info' => compact('platform_name', 'platform_price', 'platform_url'),
            'introduce_info' => compact('introduce_content_info', 'introduce_carousel_info'),
            'keyword_info' => compact('keywords'),
            'config_info' => compact('show_platform_price', 'can_free')
        ];

        try {
            GoodsService::updateGoods($goods_id, $data);
            return $this->successReturn();
        } catch (BusinessException $e) {
            Log4dd::error("修改商品失败:{$e->getMessage()}", $data, $e);
            return $this->errorReturn($e->getCode(), $e->getMessage());
        }
    }

}